package uk.ac.glasgow.demosocs.impl.ui;

import java.util.Date;

import uk.ac.glasgow.clui.ArgumentsDescription;
import uk.ac.glasgow.clui.CommandDescription;
import uk.ac.glasgow.clui.MinimumArguments;
import uk.ac.glasgow.clui.SystemCommand;
import uk.ac.glasgow.clui.SystemCommandException;
import uk.ac.glasgow.demosocs.Election;
import uk.ac.glasgow.demosocs.VotingSystem;

@MinimumArguments(6)
@CommandDescription("Edit the currently selected election details.")
@ArgumentsDescription("<nomination day> <nomination time> <start day> <start time> <stop day> <stop time> <electorate ID>")
public class EditElectionCommand extends SystemCommand<VotingSystem> {

	public EditElectionCommand(VotingSystem facade) {
		super(facade);
	}

	@Override
	public String processCommand(String[] arguments)
			throws SystemCommandException {
		boolean available = false;
		System.out.println("Available electorate IDs: 10, 11, 12");
		Date openNominations = DateUtil.parseArgumentDate(arguments[0],arguments[1]);
		Date start = DateUtil.parseArgumentDate(arguments[2],arguments[3]);
		Date stop = DateUtil.parseArgumentDate(arguments[4],arguments[5]);
		
		String electorateID = arguments[6];
		Election currentElection = facade.getCurrentElection();
		if (currentElection == null) {
			return "Failed to edit election. Current Election is not set";
		}
		
		String[] availableIDs = {"12","10","11"};
		for(String str: availableIDs)
			if(electorateID.equals(str))
				available = true;
		
		if(!available){
			return "Failed to edit election. No available electorateID has been provided";
		}
		
		if (facade.getCurrentElection().equals(currentElection)){
			return "Failed to edit election details. Are you a manager?";
		}
		
		facade.editElection(openNominations, start, stop, electorateID);
/*
		if (facade.getCurrentElection().equals(currentElection)){
			return "Failed to edit election details. Are you a manager?";
		}
		
*/		
		return "Edited election details.";
	}
}
